package com.literature.search.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.literature.search.core.common.MessageConstant;
import com.literature.search.core.entity.R.AjaxJson;
import com.literature.search.core.entity.User;
import com.literature.search.core.entity.dto.BasePageDTO;
import com.literature.search.core.utils.sp.SpUserUtil;
import com.literature.search.user.dto.LoginUserDTO;
import com.literature.search.user.dto.RegistryUserDTO;
import com.literature.search.user.dto.UserDTO;
import com.literature.search.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: DangKong
 * @Date: 2023/3/1 20:07
 * @Description:
 */

@Api(tags = "用户信息")
@RestController
@RequestMapping("/user/")
public class UserController {
    static final Map<String, String> fieldRegx = new HashMap<>(8);

    static {
        // 1-64个数字字母下划线
        fieldRegx.put("password", "^[0-9a-z_]{1,64}$");
        fieldRegx.put("phone", "/^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$/");
        fieldRegx.put("mail", "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
        // 1-140个任意字符
        fieldRegx.put("description", "(.*){1,140}");
    }

    @Autowired
    private IUserService userService;

    @PostMapping("login")
    public AjaxJson login(@RequestBody @Validated LoginUserDTO user) {
        return userService.login(user);
    }


    @PostMapping("register")
    @ApiOperation(value = "register", notes = "这里用户注册成功后会返回用户的基本信息，其中头像为默认头像，是一个grifsid，需要前端根据这个id去调用file服务的接口获取头像")
    public AjaxJson register(@Valid @RequestBody RegistryUserDTO user) {
        return AjaxJson.getSuccessData(userService.register(user));
    }

    @ApiOperation(value = "注册管理员账号")
    @PostMapping("registerAdmin")
    public AjaxJson registerAdmin(@Valid @RequestBody RegistryUserDTO user) {
        return userService.registerAdmin(user);
    }

    @ApiOperation(value = "根据用户id查询用户信息", notes = "测试版")
    @GetMapping("getUserById")
    public AjaxJson getUserInfo(@RequestParam("id") String userId) {
        return userService.getUserInfoById(userId);
    }

    @ApiOperation(value= "更新用户信息", notes = "测试版 TODO：封装校验方法、用户邮箱绑定、密码修改等")
    @SaCheckLogin
    @PostMapping("updateUser")
    public AjaxJson updateUserInfo(@Valid @RequestBody UserDTO userDTO) {
        return userService.updateUserInfo(userDTO);
    }


    @ApiOperation(value = "获取当前用户信息", notes = "必须要登录后才能获取")
    @SaCheckLogin
    @PostMapping("getCurrentUser")
    public AjaxJson getCurrentUser() {
        User currUser = SpUserUtil.getCurrUser();
        return AjaxJson.getSuccessData(currUser);
    }

    @ApiOperation(value = "用户更换头像")
    @SaCheckLogin
    @PostMapping("changeAvatar")
    public AjaxJson changeAvatar(@RequestParam("file") MultipartFile file) {
        return userService.changeAvatar(file);
    }

    @ApiOperation(value = "获取用户头像")
    @SaCheckLogin
    @PostMapping("getAvatar/{avatar}")
    public ResponseEntity<Object> getAvatar(@PathVariable String avatar)  {
        if (StringUtils.hasText(avatar)) {
            byte[] bytes = userService.getAvatarBytes(avatar);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "avatar=" + avatar)
                    .header(HttpHeaders.CONTENT_TYPE, "image/png")
                    .header(HttpHeaders.CONTENT_LENGTH, bytes.length + "")
                    .body(bytes);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(MessageConstant.USER_NOT_EXIST);
        }
    }

    @ApiOperation(value = "屏蔽用户", notes = "管理员才能调用",tags = "admin")
    @SaCheckRole("admin")
    @PostMapping("banUser")
    public AjaxJson banUser(@RequestParam List<String> userIds){
        return userService.blockUser(userIds);
    }

    @ApiOperation(value = "分页查询用户列表", notes = "管理员才能调用",tags = "admin")
    @SaCheckRole("admin")
    @PostMapping("getUserListByPage")
    public AjaxJson getUserListByPage(@ModelAttribute("pageParams") @Valid BasePageDTO page){
        return userService.getUserListByPage(page);
    }





}

